import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter} from '@/router'
import { MessageBox } from 'element-ui'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/mECo2RefIzQJBaZoibKdvicWcGqQOQPWjwjF1SicqSLqdxobNQoVPS7NAQsNacnicszbQ8G6gRc7MY378aibJHwVMBA/132',
    roles: [],
    userInfo: {}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  }
}

const actions = {
  // 用户登录
  login({ commit , dispatch}, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        // let user = decodeURIComponent(escape(window.atob(response.data.split('.')[1]))) 
        commit('SET_TOKEN', response.data)
        setToken(response.data)
        router.push('/');
        resolve();
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        let userInfo = response.data
        if (!userInfo.userId) {
          reject('验证失败，请重新登录.')
        }
        // let roles = userInfo['roleCodeList']
        // userInfo.authorities = [ 
        // "menu:root",
        // "menu:menu100",
        // "menu:menu101",
        // "menu:menu102",
        // "menu:menu103",
        // "menu:menu101",
        // "menu:menu200",
        // "menu:menu201",
        // "menu:menu300",
        // "menu:menu301",
        // "menu:menu302",
        // "menu:menu400",
        // "menu:menu401",
        // "menu:menu402",
        // "menu:menu403",
        // "menu:menu500",
        // "menu:menu501",
        // "menu:menu502",
        // "menu:menu503",
        // "menu:menu600",
        // "menu:menu601",
        // "menu:menu700",
        // "menu:menu701",
        // "menu:menu702",
        // "menu:menu703",
        // "menu:menu704",
        // ]
        commit('SET_USERINFO', userInfo) 
        commit('SET_ROLES', userInfo.authorities)
        commit('SET_NAME', userInfo['userName'])        
        resolve(userInfo.authorities)     
        // resolve(roles)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 用户登出
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() //删除token
      resetRouter()
      commit('RESET_STATE')
      //调用tagsView模块，删除切换面包屑
      this.dispatch("tagsView/delAllVisitedViews", { root: true })
      resolve()
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() 
      commit('RESET_STATE')
      resetRouter()
      //调用tagsView模块，删除切换面包屑
      this.dispatch("tagsView/delAllVisitedViews", { root: true })
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

